package com.webserver.controller;

import com.webserver.entity.User;
import com.webserver.http.HttpServletRequest;
import com.webserver.http.HttpServletResponse;

import java.io.*;

/**
 * 用于处理用户相关业务
 * @author Chen
 */
public class UserController {

    private static File userDir;

    static {
        userDir = new File("./users");
        if (!userDir.exists()) {
            userDir.mkdirs();
        }
    }

    public void reg(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("开始处理用户注册！");

        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String nickname = request.getParameter("nickname");
        String ageStr = request.getParameter("age");
        System.out.println(username + "," + password + "," + nickname + "," + ageStr);
        if (username == null || username.isEmpty()
                || password == null || password.isEmpty()
                || nickname == null || nickname.isEmpty()
                || ageStr == null || ageStr.isEmpty()
                || !ageStr.matches("[0-9]+")
        ) {
            System.out.println("用户信息输入错误，注册失败！");
            response.sendRedirect("/reg_info_error.html");
            return;
        }

        int age = Integer.parseInt(ageStr);
        File file = new File(userDir, username + ".obj");
        if (file.exists()) {
            System.out.println("用户已存在，注册失败！");
            response.sendRedirect("/have_user.html");
            return;
        }

        User user = new User(username, password, nickname, age);
        try (
                FileOutputStream fos = new FileOutputStream(file);
                ObjectOutputStream oos = new ObjectOutputStream(fos);
        ) {
            oos.writeObject(user);
            System.out.println("用户注册成功！");
            response.sendRedirect("/reg_success.html");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public void list(HttpServletRequest request, HttpServletResponse response) {
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        if (username == null || username.isEmpty() || password == null || password.isEmpty()) {
            System.out.println("用户输入信息错误!");
            response.sendRedirect("/login_info_error.html");
            return;
        }

        System.out.println("登录用户：" + username + ", 密码：" + password);

        File file = new File(userDir, username + ".obj");

        if (!file.exists()) {
            System.out.println("用户登录输入信息错误!");
            response.sendRedirect("/login_info_error.html");
            return;
        }

        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file))) {
            User user = (User) ois.readObject();
            String p = user.getPassword();
            if (p.equals(password)) {
                System.out.println("用户登录成功!");
                response.sendRedirect("/login_success.html");
                return;
            }
            System.out.println("用户登录失败！");
            response.sendRedirect("/login_fail.html");
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}
